﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;

namespace Human
{
	public class frmInthe : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox cboDonvi;
		private System.Windows.Forms.TextBox txtManv;
		private System.Windows.Forms.TextBox txtHoten;
		private System.Windows.Forms.Button cmdOK;
		private System.Windows.Forms.Button cmdCancel;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
        private AccessData mDB = new AccessData();
        string m_user = "", sql = "",s_bophan="",user_dn="";

		private System.ComponentModel.Container components = null;

		public frmInthe(string s_makp,string s_userdn)
		{
			InitializeComponent();
            s_bophan = s_makp;
            user_dn = s_userdn;
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmInthe));
            this.cboDonvi = new System.Windows.Forms.ComboBox();
            this.txtManv = new System.Windows.Forms.TextBox();
            this.txtHoten = new System.Windows.Forms.TextBox();
            this.cmdOK = new System.Windows.Forms.Button();
            this.cmdCancel = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // cboDonvi
            // 
            this.cboDonvi.Location = new System.Drawing.Point(99, 16);
            this.cboDonvi.Name = "cboDonvi";
            this.cboDonvi.Size = new System.Drawing.Size(168, 21);
            this.cboDonvi.TabIndex = 0;
            this.cboDonvi.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboDonvi_KeyDown);
            // 
            // txtManv
            // 
            this.txtManv.Location = new System.Drawing.Point(99, 40);
            this.txtManv.Name = "txtManv";
            this.txtManv.Size = new System.Drawing.Size(72, 20);
            this.txtManv.TabIndex = 1;
            this.txtManv.Validated += new System.EventHandler(this.txtManv_Validated);
            this.txtManv.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtManv_KeyDown);
            // 
            // txtHoten
            // 
            this.txtHoten.BackColor = System.Drawing.SystemColors.HighlightText;
            this.txtHoten.Enabled = false;
            this.txtHoten.Location = new System.Drawing.Point(99, 64);
            this.txtHoten.Name = "txtHoten";
            this.txtHoten.Size = new System.Drawing.Size(168, 20);
            this.txtHoten.TabIndex = 2;
            this.txtHoten.Validated += new System.EventHandler(this.txtHoten_Validated);
            this.txtHoten.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtHoten_KeyDown);
            // 
            // cmdOK
            // 
            this.cmdOK.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cmdOK.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.cmdOK.Image = ((System.Drawing.Image)(resources.GetObject("cmdOK.Image")));
            this.cmdOK.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.cmdOK.Location = new System.Drawing.Point(60, 102);
            this.cmdOK.Name = "cmdOK";
            this.cmdOK.Size = new System.Drawing.Size(88, 28);
            this.cmdOK.TabIndex = 3;
            this.cmdOK.Text = "&In";
            this.cmdOK.Click += new System.EventHandler(this.cmdOK_Click);
            // 
            // cmdCancel
            // 
            this.cmdCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cmdCancel.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.cmdCancel.Image = ((System.Drawing.Image)(resources.GetObject("cmdCancel.Image")));
            this.cmdCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.cmdCancel.Location = new System.Drawing.Point(156, 102);
            this.cmdCancel.Name = "cmdCancel";
            this.cmdCancel.Size = new System.Drawing.Size(88, 28);
            this.cmdCancel.TabIndex = 4;
            this.cmdCancel.Text = "&Thoát";
            this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);
            // 
            // label1
            // 
            this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.label1.Location = new System.Drawing.Point(41, 17);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(56, 16);
            this.label1.TabIndex = 5;
            this.label1.Text = "Bộ phận :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label2
            // 
            this.label2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.label2.Location = new System.Drawing.Point(17, 40);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(80, 16);
            this.label2.TabIndex = 6;
            this.label2.Text = "Mã nhân viên :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label3
            // 
            this.label3.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.label3.Location = new System.Drawing.Point(17, 65);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(80, 16);
            this.label3.TabIndex = 7;
            this.label3.Text = "Họ tên :";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // frmInthe
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(314, 143);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.cmdCancel);
            this.Controls.Add(this.cmdOK);
            this.Controls.Add(this.txtHoten);
            this.Controls.Add(this.txtManv);
            this.Controls.Add(this.cboDonvi);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmInthe";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "In thẻ nhân viên";
            this.Load += new System.EventHandler(this.frmInthe_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void cmdCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void frmInthe_Load(object sender, System.EventArgs e)
		{
            m_user = mDB.user;
            DataSet ds = new DataSet();
			ds = mDB.get_data("select * from "+m_user+".bophan");
			cboDonvi.DisplayMember="tenbp";
			cboDonvi.ValueMember="madv";
			cboDonvi.DataSource=ds.Tables[0];
		}

		private void cboDonvi_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter)
				SendKeys.Send("{Tab}");
		}

		private void txtManv_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter)
				SendKeys.Send("{Tab}");
		}

		private void txtHoten_Validated(object sender, System.EventArgs e)
		{
			
		}

		private void txtManv_Validated(object sender, System.EventArgs e)
		{
			DataSet dstemp = new DataSet();
			string ma_nv="";
			txtManv.Text = txtManv.Text.PadLeft(5,'0');
			if (txtManv.Text!="")
			{
				ma_nv=txtManv.Text;
				dstemp=mDB.get_data("SELECT manv,"+
					" trim(ho)||' '||trim(ten) AS Hoten"+
                    " FROM " + m_user + ".llnv WHERE trim(manv)='" + ma_nv.Trim() + "'");
                if (dstemp.Tables[0].Rows.Count > 0)
                    txtHoten.Text = dstemp.Tables[0].Rows[0]["hoten"].ToString();
                else
                {
                    MessageBox.Show("Không tìm thấy!", AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtHoten.Text = "";
                    txtManv.Focus();
                    return;
                }
			}
			else
				txtHoten.Text="";

		}

		private void txtHoten_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter)
				SendKeys.Send("{Tab}");
		}

		private void cmdOK_Click(object sender, System.EventArgs e)
		{
			string m_bp="";
            
			sql="SELECT a.manv, Trim(a.ho)||' '||Trim(a.ten) AS hoten,a.hinhanh,a.barcode,";
			sql+=" Trim(diengiai) AS chucvu, d.ten AS chucdanh,a.madv, e.tenbp ";
            sql += " FROM (((" + m_user + ".llnv a INNER JOIN " + m_user + ".hopdong b ON a.manv = b.manv) ";
            sql += " INNER JOIN " + m_user + ".dmcv c ON b.macv = c.macv) ";
            sql += " INNER JOIN " + m_user + ".dmcd d ON a.macd = d.ma) ";
            sql += " INNER JOIN " + m_user + ".bophan e ON a.madv = e.madv ";

            if (cboDonvi.SelectedIndex != -1)
                m_bp = cboDonvi.SelectedValue.ToString();
            if (m_bp != "")
                sql += " where a.madv='" + m_bp + "'";
            if (txtManv.Text != "")
            {
                if (m_bp != "")
                    sql += " and a.manv='" + txtManv.Text + "'";
                else
                    sql += " where a.manv='" + txtManv.Text + "'";
            }
			DataSet ds = new DataSet();
			ds=mDB.get_data(sql);
			//ds.WriteXml(@"xml\thenv.xml",XmlWriteMode.WriteSchema);
			frmReport f=new frmReport(mDB,ds,"","rThenhanvien.rpt",true,user_dn);
			f.ShowDialog(this);

		}
	}
}
